<?php
/**
 * 微信模版工具类
 *
 * 微信模版工具类的注意及说明
 * 以下use模块请不要互相伤害
 *
 * @author    xiecz <xiecz@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Services\Tool;
use Log;
class WxTemplate{

    const sendTemplateMessage = "https://api.weixin.qq.com/cgi-bin/message/template/send";
    const getAccessToken = "http://wx.appyys.com/get-token";
    const newAccessToken = "http://wx.appyys.com/get-refresh-token";
    /**

     */
    public static function send($input){

        $data = array(
            'touser' => $input['open_id'],
            'template_id' =>  $input['template_id'],
            'url' => '',
            'data' => [
                'first' => [
                    'value' => $input['title'],
                    'color' => '#000000'
                ],
                'keyword1' => [
                    'value' => $input['body'],
                    'color' => '#000000'
                ],
                'keyword2' => [
                    'value' => $input['total_fee'],
                    'color' => '#000000'
                ],
                'keyword3' => [
                    'value' => $input['name'],
                    'color' => '#000000'
                ],
                'keyword4' => [
                    'value' => $input['phone'],
                    'color' => '#000000'
                ],
                'keyword5' => [
                    'value' => $input['order_id'],
                    'color' => '#000000'
                ],
                'remark' => [
                    'value' => '请及时处理',
                    'color' => '#000000'
                ]
            ]
        );
        self::sendMessage(json_encode($data));
    }

    /**
     * 发送模板消息
     * @param $data
     * @return bool
     */
    public static function sendMessage($data){
        $url = self::sendTemplateMessage.'?access_token='.self::getAccessToken(self::getAccessToken);

        $resultJson = self::https_request($url,$data);
        $result = json_decode($resultJson);
        if($result->errcode == '0'){
            return $result->msgid;
        }else{
            Log::info(date('y-m-d h:i:s',time()).':'.json_encode($result));
            return false;
        }
    }

    //获取AccessToken
    static function getAccessToken($url){
        $result = self::https_request($url,'',0);
        $result = json_decode($result);
        if(!$result || !isset($result->AccessToken)){
            $result = self::https_request(self::newAccessToken,'',0);
        }
        return $result && isset($result->AccessToken) ?
               $result->AccessToken : '';


    }

    //微信接口数据传输的万能函数，几乎适应于所有微信接口数据的访问及提交(呵呵)
    public static function https_request($url,$data = null,$type = 1)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, $type);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
}
